package com.wxd.framework.common.web.session;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class HttpSessionProvider implements SessionProvider  {

	@Override
	public Serializable getAttribute(HttpServletRequest request, String name) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			return (Serializable) session.getAttribute(name);
		} else {
			return null;
		}
	}

	@Override
	public void setAttribute(HttpServletRequest request, HttpServletResponse response, String name,
			Serializable value) {
		HttpSession session = request.getSession();
		session.setAttribute(name, value);		
	}

	@Override
	public String getSessionId(HttpServletRequest request, HttpServletResponse response) {
		return request.getSession().getId();
	}

	@Override
	public void logout(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			session.invalidate();
		}		
	}

	@Override
	public void setAttributeObject(HttpServletRequest request, HttpServletResponse response, String name,
			Object value) {
		HttpSession session = request.getSession();
		session.setAttribute(name, value);		
	}

	@Override
	public Object getAttributeObject(HttpServletRequest request, String name) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			return (Object) session.getAttribute(name);
		} else {
			return null;
		}
	}

	@Override
	public void removeAttribute(HttpServletRequest request, HttpServletResponse response, String name) {
		HttpSession session = request.getSession();
		session.removeAttribute(name);
	}

}
